package main

import (
	"gointro/pipeline"
	"fmt"
	"os"
	"bufio"
)

func main()  {
	const filename = "large.in"
	const n = 100000000
	file, err := os.Create(filename)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	p := pipeline.RandomSource(n)
	writer := bufio.NewWriter(file)
	pipeline.WriteSink(writer, p)
	writer.Flush()

	file, err = os.Open(filename)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	p = pipeline.ReaderSource(bufio.NewReader(file), -1)
	count := 0
	for v:= range p {
		fmt.Println(v)
		count++
		if count>=100 {
			break
		}
	}
}

func MergeDemo() {
	p := pipeline.Merge(
		pipeline.InMemSort(
			pipeline.ArraySource(3,2,6,4,9,7)),
		pipeline.InMemSort(
			pipeline.ArraySource(13,21,6,14,91,17)))

	for v:= range p {
		fmt.Println(v)
	}
	/*
	for {
		if num, ok := <-p; ok {
			fmt.Println(num)
		} else {
			break
		}
	}
	*/

}
